package com.kitty.game.equip.handler;

import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;

import com.kitty.game.config.Equip;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.message.RespHunQiOptRes;
import com.kitty.game.equip.model.HunQiAttrField;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.NotifyModules;
import com.kitty.mina.message.MessagePusher;
import java.text.MessageFormat;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.kitty.game.equip.handler.UpgradeHandler;

/**
 * 魂器 混沌
 */
@Component("EquipHunQiHunDunHandler")
@Slf4j
public class EquipHunQiHunDunHandler extends UpgradeHandler {

    public static final String EXCEPTION = "失败";

    public static final String CRYSTAL = "天倾石";

    /**
     * 炼化成功提示
     */
    private static final String REFINE_SUCCESS = "恭喜你，炼化成功。";

    /**
     * 炼化失败提示
     */
    private static final String REFINE_FAIL = "真遗憾，炼化失败了。";

    private static final Short ATTR_TYPE = 260;

    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip roleEquip = (RoleEquip) params[0];
        String param = (String) params[1];
        Equip equip = (Equip) params[2];

        Random random = ThreadLocalRandom.current();
        String[] split = param.split("\\|");
        boolean isUseLimit;
        int size = 1;
        boolean success = true;

        isUseLimit = true;

        /**扣钱*/
        int cash = needCash(roleEquip.queryLevel(), 0);
        roleService.subtractMoney(role, cash);
        int limitNum = 0;
        /**删除道具*/
        limitNum += equipService.delRoloEquip(CRYSTAL, size, isUseLimit, role);

        /**添加限制时间*/
        if (limitNum >= 1) {
            roleEquip.addLimitTime(limitNum);
        }
        if (success) {
            /**获得随机属性*/
            int pos = Byte.parseByte(split[0]);
            HunQiAttrField attrField = roleEquip.getHunQiFields().get(ATTR_TYPE).getField().get(pos - 1);
            int point = random.nextInt(50) + 51;
            attrField.setHunDun((byte) point);
            refreshRoleEquip(role, roleEquip);
            //不计算完美度
            //equipService.countPerfect(roleEquip);
            RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
            respGeneralNotify.setValue(roleEquip.getId() + "");
            respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_EQUIP_REFINE_OK);//炼化成功
            MessagePusher.pushMessage(role, respGeneralNotify);

            //安卓引动混沌出错 是因为没有发送完成通知
//            RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
//            respGeneralNotify.setValue(35 + "");
//            respGeneralNotify.setNotify((short)50034);//炼化成功
//            MessagePusher.pushMessage(role, respGeneralNotify);
            RespHunQiOptRes hunQiOptRes = new RespHunQiOptRes();
            hunQiOptRes.setBackpackPos(roleEquip.getPosition());
            hunQiOptRes.setPos((byte) pos);
            MessagePusher.pushMessage(role, hunQiOptRes);
            roleService.addAchieveScore(role,"304002");
            return REFINE_SUCCESS;
        } else {
            return REFINE_FAIL;
        }
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, upgradePos);
        if (roleEquip == null) {
            return "请放入正确的装备。";
        }
        if (!isRightPosition(roleEquip.queryEquipPos(false))) {
            return MessageFormat.format(NO_RIGHT_POSITION, "炼化混沌", roleEquip.getName());
        }
        Equip equip = EquipDataPool.getByName(roleEquip.getName());
        if (equip == null) {
            log.error("找不到装备配置ICON=[{}]", upgradePos);
            return MessageFormat.format(NOT_FIND_CONFIG, roleEquip.getName());
        }

        String noEnough = checkResource(role, param, roleEquip);
        if (noEnough != null) {
            return noEnough;
        }

        return doUpgrade(role, roleEquip, param, equip);
    }

    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        String param = (String) objects[1];
        RoleEquip roleEquip = (RoleEquip) objects[2];

        //1|46 数组位置
        String[] split = param.split("\\|");
        if (split.length < 2) {
            return EXCEPTION;
        }

        int pos = Integer.parseInt(split[0]);

        if (!roleEquip.getName().startsWith("魂器")) {
            return EXCEPTION;
        }

        HunQiAttrField attrField = roleEquip.getHunQiFields().get(ATTR_TYPE).getField().get(pos - 1);
        //如果已经炼化
        if (attrField.getYangRate() > 0) {
            attrField.setYangRate((byte) 0);
            attrField.setYangAttrName("");
            attrField.setYangAttrVal((short) 0);
            attrField.setYinAttrName("");
            attrField.setYinAttrVal((short) 0);
        }

        /**天倾石*/
        int crystalNum = equipService.getCount(CRYSTAL, true, role);
        if (crystalNum < 1) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, CRYSTAL);
        }

        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        return 0;
    }

    @Override
    public String getUpdateTypeStr() {
        return "hun_qi_hun_dun";
    }
}
